package com.agent.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.agent.entity.User;
import com.agent.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/*
    后台管理系统 -- 用户管理
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/user")
public class UserController {

    private final UserService userService;

    // 新增用户
    @PostMapping
    public User create(@RequestBody User user) {
        userService.save(user);
        return user;
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public User getById(@PathVariable Long id) {
        return userService.getById(id);
    }

    // 修改用户
    @PutMapping
    public boolean update(@RequestBody User user) {
        if (user.getId() == null) return false;
        return userService.updateById(user);
    }

    // 删除用户（逻辑删除）
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return userService.removeById(id);
    }

    // 分页查询（可按用户名、状态筛选，排除逻辑删除由MP自动处理）
    @GetMapping("/page")
    public Page<User> page(@RequestParam(defaultValue = "1") long page,
                           @RequestParam(defaultValue = "10") long size,
                           @RequestParam(required = false) String username,
                           @RequestParam(required = false) Integer stated) {
        Page<User> p = new Page<>(page, size);
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.like(username != null && !username.isEmpty(), User::getUsername, username)
          .eq(stated != null, User::getStated, stated)
          .orderByDesc(User::getId);
        return userService.page(p, qw);
    }
}
